
var page = 0;
var pageShowCount = 15;

var searchText = '';
var provinceCode = '';
var orgId = '';

$(function() {
    getProvinceList();
    search();
});

function search() {
	getPageCount();
}

function enterIn(evt){
   var evt=evt?evt:(window.event?window.event:null);
   if (evt.keyCode==13){
      search();
   }
}

//查询机构列表
function getOrgListByProvince(provinceCode) {
	$.ajax({
		url : '/index/project-search!getOrgListByProvince.action',
		dataType: 'json',
		data:{
			"provinceCode": provinceCode
		},
		success : function(data) {
			Interceptor(data);
			 var contentHtml='<ul class="list_3">';
		     $(data).each(function(i) {
		        contentHtml += '<li><a href="javascript:void(0)" onclick="orgSearch(\'' 
		        			+ data[i].orgId + '\')" >' + data[i].orgName + '（' + data[i].count + '）</a></li>';
		     });
		     $("#" + provinceCode + "_org").html(contentHtml + '</ul>');
		},
		error : function() {
			alert("通信失败！");
		}
	 });
}

//查询省份列表
function getProvinceList() {
	$.ajax({
		url : '/index/project-search!getProvinceList.action',
		dataType: 'json',
		success : function(data) {
			Interceptor(data);
			 var contentHtml='';
			 var provinceCode_frist='';
		     $(data).each(function(i) {
		    	contentHtml += '<div class="searchnav" id="' + data[i].provinceCode + '_province">' +
				        '<div class="BoxTitle">' +
				          '<h2><a href="javascript:void(0)" onclick="provinceSearch(\'' + data[i].provinceCode + '\')">' + data[i].provinceName + '（' + data[i].count + '）</a></h2>' +
				        '</div>' +
				        '<div class="BoxContent" id="' + data[i].provinceCode + '_org">' +
				        '</div>' +
				       ' <div class="BoxBottom">' +
				         ' <div class="BoxBottomRight">&nbsp;</div>' +
				       ' </div>'+
				      '</div>';
				 if(i==0) {
				 	provinceCode_frist = data[i].provinceCode;
				 }
		     });
		     $("#SearchLeft").html(contentHtml);
		     //初始化左侧
			 defaultProvinceSearch(provinceCode_frist);
		},
		error : function() {
			alert("通信失败！");
		}
	 });
}

function defaultProvinceSearch(id) {
	$("#" + id + "_org").show();
	getOrgListByProvince(id);
}

function provinceSearch(id){
	$("[id$='_org']").css("display","none");
	$("#" + id + "_org").show();
	provinceCode = id;
	orgId = '';
	getOrgListByProvince(provinceCode);
	search();
}

function orgSearch(id){
	orgId = id;
	search();
}


// 切换每页显示条数
function changePageShowCount(changeId){
	var selectedVar = $("#"+changeId).find("option:selected");
	pageShowCount = selectedVar.text();
	var selectedIndex = $("#"+changeId).get(0).selectedIndex;
	$("[name=pageShowCount]").get(0).selectedIndex = selectedIndex;
	$("[name=pageShowCount]").get(1).selectedIndex = selectedIndex;
	search();
}

// 查询记录总数
function getPageCount(){

	searchText = $("#searchText").val();
	
	$.ajaxq("getPageCount", {
		async : false,
		url : '/index/project-search!getPageCount.action',
		type : 'post',
		dataType : 'json',
		data : {
			"searchText" : searchText,
			"provinceCode" :provinceCode,
			"orgId" :orgId
		},
		success : function(data) {
			Interceptor(data);
			$("[name=countTotal]").html("" + data);
			showPageResult(data);
		},
		error : function() {
			// $("[name=countTotal]").html("" + data);
		}
	});
}

// 分页
function showPageResult(count) {
	if (count > 0) {
		$(".pagination").pagination(count, {
			num_edge_entries : 1, // 边缘页数
			num_display_entries : 10, // 主体页数
			prev_text : "上一页",
			next_text : "下一页",
			current_page : page,
			callback : showPageResultCallback,
			items_per_page : pageShowCount
		});
		$(".pagination").show();
	} else {
		$(".pagination").hide();
		var resultContent = '';
		resultContent +='<tr>'
						+'<th align="left" scope="col">注册号</th>'
						+'<th align="left" scope="col">注册题目</th>'
						+'<th align="left" scope="col">研究类型</th>'
						+'<th align="left" scope="col">注册时间</th>'
					    +'</tr><tr><td colspan="4">暂无信息</td></tr>';
		$("#dataArea").html(resultContent);
	}
}

// 返回数据集合
function showPageResultCallback(page_index, jq) {
	$.ajaxq("getPageList", {
		async : false,
		url : '/index/project-search!getPageList.action',
		type : 'post',
		data : {
			"pageIndex" : page_index,
			"pageShowCount" : pageShowCount,
			"searchText" : searchText,
			"provinceCode" :provinceCode,
			"orgId" :orgId
		},
		dataType : 'json',
		beforeSend : parent.msgLoading,
		complete : parent.successLoading,
		success : function(data) {
			Interceptor(data);
			showSearchResultContent(data);
		},
		error : function() {
			alert("对不起，检索超时！");
		}
	});
	return false;
}

// 显示数据集合
function showSearchResultContent(data){
	var resultContent = '';
	resultContent +='<tr>'
					+'<th align="left" scope="col">注册号</th>'
					+'<th align="left" scope="col">注册题目</th>'
					+'<th align="left" scope="col">研究类型</th>'
					+'<th align="left" scope="col">注册时间</th>'
				    +'</tr>';
	if(null!=data){
		$(data).each(function(i) {
			var dataObj = data[i];
			
			var id = dataObj.id;
	        var name = dataObj.name;
	        var regNumber = dataObj.regNumber;
	        var organizationName = dataObj.organizationName;
	        var createTime = dataObj.createTime;
	        var researchType = dataObj.researchType;
	        
	        name = null==name?"":name;
	        regNumber = null==regNumber?"":regNumber;
	        organizationName = null==organizationName?"":organizationName;
	        createTime = null==createTime?"":createTime;
	        researchType = null==researchType?"":researchType;
	        
	        var detailUrl = "project_detail.jsp?id=" + id;
	        if(i%2==1){
	        	resultContent +='<tr>';
	        }else{
	        	resultContent +='<tr class="odd">';
	        }
    	    resultContent += '<td align="center">' + regNumber + '</td>' 
    	    				+'<td align="left"><H2><a href="' + detailUrl + '" >' + name + '</a></H2>' + organizationName + '</td>'
							+'<td align="center">' + researchType + '</td>'
							+'<td align="center">' + createTime.substring(0,10) + '</td>'
						 +'</tr>';
	        
		});
		if(data==''){
			resultContent+='<tr><td colspan="4">暂无信息</td></tr>';
		}
	}else{
		resultContent+='<tr><td colspan="4">暂无信息</td></tr>';
	}
	$("#dataArea").html(resultContent);
}

